<?php
/**
 * --------------------------------------------------------------------
 * MCore精简框架
 * 作者：阿枫，QQ：52037872，Email：support@mdeve.com
 * --------------------------------------------------------------------
 *
 * SVN revision information:
 * @version $Revision: 1 $:
 * @author  $Author: zy $:
 * @date      $Date: 2017-03-10 21:12:21 +0800 (星期五, 10 三月 2017) $:
 */

require_once dirname(__FILE__) . "/abstract_cache.php";

class CacheFile extends _cache_interface {

    private $_prefix = '';
    private $_cache_dir = '';
    public $_enable;

    function __construct(){
        global $config;
        $this->_prefix = $config['memory']['prefix'];
        $this->_cache_dir = DATA_PATH."/runtime/cache";
        if(!file_exists($this->_cache_dir)) mkdir($this->_cache_dir);
        $this->_enable = is_writable($this->_cache_dir) ? true : false;
    }

    /**
     * 返回是否支持缓存，在使用前先判断，否则不支持的话强行使用会出错。
     * @return boolean
     */
    function is_support(){
        return $this->_enable;
    }


    /**
     * 判断缓存变量是否存在
     * @param string $key
     * @return bool
     */
    public function exists($key){
        $_cache_file = $this->_cache_dir. "/". md5($this->_storageKey ( $key ));
        if(!file_exists($_cache_file)) return false;
        // 读取文件，判断是否超时，如果超时则删掉
        $data = file_get_contents($_cache_file);
        $property = substr($data, 0, strpos($data, "|"));
        list($type, $expire) = explode(",", $property);
        // 获取缓存时间
        $create_time = filectime($_cache_file);
        if($expire>0){	// 如果有过期时间
            if(time()-$create_time > $expire) {		// 已超时
                unlink($_cache_file);
                return false;
            }
        }

        return true;
    }


    /**
     * 读缓存变量
     *
     * @param string $key
     * @return mixed
     */
    public function fetch($key) {
        if(!$this->_enable) $this->_err();
        $_cache_file = $this->_cache_dir. "/". md5($this->_storageKey ( $key ));
        if(!file_exists($_cache_file)) return false;
        $data = file_get_contents($_cache_file);
        $property = substr($data, 0, strpos($data, "|"));
        list($type, $expire) = explode(",", $property);
        // 获取缓存时间
        $create_time = filectime($_cache_file);
        if($expire>0){	// 如果有过期时间
            if(time()-$create_time > $expire) {		// 已超时
                unlink($_cache_file);
                return false;
            }
        }
        $data = substr($data, strpos($data, "|")+1);
        if($type == "arr") $data = unserialize($data);
        return $data;
    }

    /**
     * 缓存变量
     *
     * @param string $key
     * @param string $value
     * @return bool
     */
    public function store($key, $value, $time=0) {
        if(!$this->_enable) $this->_err();
        $_cache_file = $this->_cache_dir. "/". md5($this->_storageKey ( $key ));
        if(file_exists($_cache_file)) unlink($_cache_file);
        if(is_array($value)) $value = "arr,{$time}|". serialize($value);
        else $value = "str,{$time}|". $value;
        file_put_contents($_cache_file, $value);
        return true;
    }

    /**
     * 删除缓存变量
     *
     * @param string $key
     * @return bool
     */
    public function delete($key) {
        if(!$this->_enable) $this->_err();
        $_cache_file = $this->_cache_dir. "/". md5($this->_storageKey ( $key ));
        if(file_exists($_cache_file)){
            unlink($_cache_file);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 清(删)除所有缓存
     *
     * @param string $cache_type 缓存类型，默认为 user ，否则系统缓存(缓存文件)将被清除
     * @return bool
     */
    public function clear($cache_type="user") {

        if(!$this->_enable) $this->_err();
        $fp=opendir($this->_cache_dir);
        while (false !== ($fstr=readdir($fp))){
            if ($fstr != "." && $fstr != "..") {
                if(is_file($this->_cache_dir .'/'.$fstr)){
                    unlink($this->_cache_dir .'/'.$fstr);
                }
            }
        }

        return true;
    }

    public function server_info(){
        return null;
    }

    private function _storageKey($key) {
        return $this->_prefix . '_' . $key;
    }

    private function _err(){
        halt ( 'file extension didn\'t installed' );
    }

}